package service

import (
	"encoding/json"
	"fmt"

	"zhonghui/console/warehouse/cfgx"
	"zhonghui/console/warehouse/logx"

	"github.com/ddliu/go-httpclient"
	jsoniter "github.com/json-iterator/go"
)

type ContactConfigRequest struct {
	Type          int32    `json:"type"`
	Scene         int32    `json:"scene"`
	Style         int32    `json:"style,omitempty"`
	IsTemp        bool     `json:"is_temp,omitempty"`
	ExpiresIn     int      `json:"expires_in,omitempty"`
	ChatExpiresIn int      `json:"chat_expires_in,omitempty"`
	IsExclusive   bool     `json:"is_exclusive,omitempty"`
	Party         []int    `json:"party,omitempty"`
	Remark        string   `json:"remark,omitempty"`
	SkipVerify    bool     `json:"skip_verify,omitempty"`
	State         string   `json:"state,omitempty"`
	Unionid       string   `json:"unionid,omitempty"`
	User          []string `json:"user,omitempty"`
	// Conclusions   ContactConfigRequestConclusions `json:"conclusions,omitempty"`
}

// type ContactConfigRequestConclusions struct {
// 	Image       ContactConfigRequestConclusionsImage       `json:"image"`
// 	Link        ContactConfigRequestConclusionsLink        `json:"link"`
// 	Miniprogram ContactConfigRequestConclusionsMiniprogram `json:"miniprogram"`
// 	Text        ContactConfigRequestConclusionsText        `json:"text"`
// }

// type ContactConfigRequestConclusionsImage struct {
// 	MediaID string `json:"media_id"`
// }

// type ContactConfigRequestConclusionsLink struct {
// 	Desc   string `json:"desc"`
// 	Picurl string `json:"picurl"`
// 	Title  string `json:"title"`
// 	URL    string `json:"url"`
// }

// type ContactConfigRequestConclusionsMiniprogram struct {
// 	Appid      string `json:"appid"`
// 	Page       string `json:"page"`
// 	PicMediaID string `json:"pic_media_id"`
// 	Title      string `json:"title"`
// }

// type ContactConfigRequestConclusionsText struct {
// 	Content string `json:"content"`
// }

type CreateContactConfigResponse struct {
	BaseResp
	ConfigID string `json:"config_id"`
	QrCode   string `json:"qr_code"`
}

func CreateContactConfig(accessToken string, params ContactConfigRequest) (*CreateContactConfigResponse, error) {
	resp := CreateContactConfigResponse{}
	host := cfgx.Cfg().GetString("wx.host")
	url := fmt.Sprintf("%s/cgi-bin/externalcontact/add_contact_way?access_token=%s", host, accessToken)

	jsonBody, err := jsoniter.MarshalToString(params)
	if err != nil {
		return nil, err
	}
	x, _ := json.Marshal(params)
	logx.Zap().Infow("CreateContactConfig", "url", url, "jsonBody", string(x))
	res, err := httpclient.PostJson(url, jsonBody)
	if err != nil {
		return nil, err
	}
	ret, err := res.ToString()
	if err != nil {
		return nil, err
	}
	return &resp, jsoniter.UnmarshalFromString(ret, &resp)
}

type UpdateContactConfigRequest struct {
	ConfigId string   `json:"config_id"`
	User     []string `json:"user,omitempty"`
}

func UpdateContactConfig(accessToken string, params UpdateContactConfigRequest) (*BaseResp, error) {
	resp := BaseResp{}
	host := cfgx.Cfg().GetString("wx.host")
	url := fmt.Sprintf("%s/cgi-bin/externalcontact/update_contact_way?access_token=%s", host, accessToken)

	jsonBody, err := jsoniter.MarshalToString(params)
	if err != nil {
		return nil, err
	}
	x, _ := json.Marshal(params)
	logx.Zap().Infow("UpdateContactConfig", "url", url, "jsonBody", string(x))
	res, err := httpclient.PostJson(url, jsonBody)
	if err != nil {
		return nil, err
	}
	ret, err := res.ToString()
	if err != nil {
		return nil, err
	}
	return &resp, jsoniter.UnmarshalFromString(ret, &resp)
}
